/** https://www.acwing.com/problem/content/156/ */
#include<bits/stdc++.h>
using namespace std;
deque<int> q;
const int maxn = 1e6+7;
int a[maxn];
int n, k;
int main() {
  cin>>n>>k;
  for(int i=0; i<n; ++i) cin>>a[i];
  /** MIN */
  for(int i=0; i<n; ++i) {
    int now = a[i];
    while(q.size() && i - q.front() >= k) q.pop_front();
    // if(q.size() && now<=a[q.front()]) {
    //   q.clear();
    // }
    while(q.size() && a[q.back()]>=now) q.pop_back();
    q.push_back(i); // 保存下标
    if(i >= k-1) cout<<a[q.front()]<<' ';
  }
  cout<<'\n';
  q.clear();
  /** MAX */
  for(int i=0; i<n; ++i) {
    int now = a[i];
    while(q.size() && i-q.front()>=k) q.pop_front();
    while(q.size() && a[q.back()]<=now) q.pop_back();
    q.push_back(i);
    if(i>=k-1) cout<<a[q.front()]<<' ';
  }
  cout<<'\n';
}
